import { createRouter, createWebHistory } from 'vue-router'

const Layout = () => import('../views/layout/Layout.vue')
const Major = () => import('../views/login/Major.vue')
const Home = () => import('../views/home/Home.vue')
const Focus = () => import('../views/focus/Focus.vue')
const Space = () => import('../views/space/Space.vue')
const Setting = () => import('../views/setting/Setting.vue')

const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      { path: '/', component: Home },
      { path: 'focus', component: Focus },
      { path: 'space/:userId', component: Space },
      { path: 'setting', component: Setting },
    ]
  },
  {
    path: '/login',
    component: Major
  },
  {
    path: '/register',
    component: Major
  },

  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  const cookie = document.cookie
  if((to.fullPath === '/login' || to.fullPath === '/register')) {
    next()
    return
  }
  if(cookie) {
    console.log(22)
    return next()
  }
  alert('尚未登录或登录过期')
  next('/login')
})

export default router
